<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>委托(delegate) | Is2</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="委托(delegate)">
<meta property="og:type" content="article">
<meta property="og:title" content="委托(delegate)">
<meta property="og:url" content="/2015/05/20/c#-delegate/index.html">
<meta property="og:site_name" content="Is2">
<meta property="og:description" content="委托(delegate)">
<meta property="og:image" content="/image/cSharp/cSharp31.png">
<meta property="og:image" content="/image/cSharp/cSharp32.png">
<meta property="og:image" content="/image/cSharp/cSharp33.png">
<meta property="og:image" content="/image/cSharp/cSharp34.png">
<meta property="og:image" content="/image/cSharp/cSharp35.png">
<meta property="og:image" content="/image/cSharp/cSharp36.png">
<meta property="og:image" content="/image/cSharp/cSharp37.png">
<meta property="og:image" content="/image/cSharp/cSharp38.png">
<meta property="og:image" content="/image/cSharp/cSharp39.png">
<meta property="og:image" content="/image/cSharp/cSharp40.png">
<meta property="og:image" content="/image/cSharp/cSharp41.png">
<meta property="og:image" content="/image/cSharp/cSharp42.png">
<meta property="og:image" content="/image/cSharp/cSharp43.png">
<meta property="og:image" content="/image/cSharp/cSharp44.png">
<meta property="og:image" content="/image/cSharp/cSharp45.png">
<meta property="og:image" content="/image/cSharp/cSharp46.png">
<meta property="og:image" content="/image/cSharp/cSharp47.png">
<meta property="og:image" content="/image/cSharp/cSharp48.png">
<meta property="og:image" content="/image/cSharp/cSharp49.png">
<meta property="og:image" content="/image/cSharp/cSharp50.png">
<meta property="og:image" content="/image/cSharp/cSharp51.png">
<meta property="og:image" content="/image/cSharp/cSharp52.png">
<meta property="og:updated_time" content="2016-10-08T12:46:00.531Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="委托(delegate)">
<meta name="twitter:description" content="委托(delegate)">
  
    <link rel="alternate" href="/atom.xml" title="Is2" type="application/atom+xml">
  
  
    <link rel="icon" href="img/logo.ico">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">

  

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" integrity="sha384-XdYbMnZ/QjLh6iI4ogqCTaIjrFk87ip+ekIjefZch0Y+PvJ8CDYtEs1ipDmPorQ+" crossorigin="anonymous">

  <link rel="stylesheet" href="/css/styles.css" type="text/css">
  

  
  <!--百度统计代码-->
  <script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?37b51d72e2b91d854971620de6fef9bc";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>
<!--百度自动推送代码-->
<script>
(function(){
    var bp = document.createElement('script');
    bp.src = '//push.zhanzhang.baidu.com/push.js';
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>
<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
</head>

<body>
  <nav class="navbar navbar-inverse">
  <div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main-menu-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="main-menu-navbar">
      <ul class="nav navbar-nav">
        
          <li><a class=""
                 href="/index.html">首页</a></li>
        
          <li><a class=""
                 href="/categories/文集/">文集</a></li>
        
          <li><a class=""
                 href="/archives/">归档</a></li>
        
          <li><a class=""
                 href="/resource/">资源</a></li>
        
          <li><a class=""
                 href="/about/">关于</a></li>
        
        <li> <a href="/atom.xml">RSS</a> </li> 
      </ul>

      <!--
      <ul class="nav navbar-nav navbar-right">
        
          <li><a href="/atom.xml" title="RSS Feed"><i class="fa fa-rss"></i></a></li>
        
      </ul>
      -->
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

  <div class="container">
    <div class="blog-header">
  <h1 class="blog-title">Is2</h1>
  
    <p class="lead blog-description">我只想做的更好</p>
  
</div>

    <div class="row">
        <div class="col-sm-8 blog-main">
          <article id="post-c#-delegate" class="article article-type-post" itemscope itemprop="blogPost">

  <header class="article-header">
    
  
    <h1 class="article-title" itemprop="name">
      委托(delegate)
    </h1>
  


  </header>

  <div class="article-meta">
    <div class="article-datetime">
  <a href="/2015/05/20/c#-delegate/" class="article-date"><time datetime="2015-05-20T08:47:52.000Z" itemprop="datePublished">2015-05-20</time></a>
</div>

    
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/c/">c#</a>
  </div>

  </div>
  <div class="article-inner">

    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="什么是委托">什么是委托</h3><p>　　<strong>委托</strong>是一个包含具有相同签名和返回值类型的有序方法列表。<br>　　■ 方法的列表称为<strong>调用列表</strong>(invocation list)<br>　　■ 当委托被调用时，它调用列表中的每一个方法。<br><img src="/image/cSharp/cSharp31.png" alt=""><a id="more"></a><br>　　包含单个方法的委托和C++的函数指针相似，然而，与函数指针不同的是，委托是面向对象的，并且是类型安全的(type-safe)的。<br>　　<br>　　<strong>“调用列表”中的方法</strong>：<br>　　由委托保存的方法可以来自任何类或者结构，只要他们同时匹配委托的如下两点：<br>　　■ 返回值<br>　　■ 签名(包括ref和out修饰符)<br>　　调用列表中的方法可以是实例方法或者静态方法。</p>
<h3 id="声明委托类型">声明委托类型</h3><p>　　委托是一种数据类型，就好像类是类型一样。与类相同，委托类型必须在被用来创建变量以及类型的对象之前进行声明，才可以使用。<br><img src="/image/cSharp/cSharp32.png" alt=""><br>　　委托类型的声明看上去和方法的声明比较相似，有返回值类型和签名，<strong>返回值类型和签名指定了委托接受的方法的形式</strong>。<br>　　上图中的代码声明了<em>MyDel</em>类型的委托。声明指定了这种类型的委托只会接受没有返回值(void)并且有单个int参数的方法。<br><img src="/image/cSharp/cSharp33.png" alt=""></p>
<p>　　<strong>委托类型声明与方法声明的两个不同方面</strong>：<br>　　■ 以<em>delegate</em>关键字开头<br>　　■ 没有方法主体</p>
<h3 id="创建委托对象">创建委托对象</h3><p>　　<strong>委托是引用类型</strong>，因此有引用和对象。我们可以声明变量并创建类型的对象。<br><img src="/image/cSharp/cSharp34.png" alt=""></p>
<p>　　有两种创建委托对象的方式：<br>　　第一种是使用带<em>new</em>运算符的对象创建表达式。<br><img src="/image/cSharp/cSharp35.png" alt=""><br>　　第二种是使用快捷语法，直接使用方法赋值。使用快捷语法方法名称和其相对应的委托类型之间有隐式转换。<br><img src="/image/cSharp/cSharp36.png" alt=""><br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">delegate</span> <span class="keyword">void</span> <span class="title">MyDel</span>(<span class="params"><span class="keyword">int</span> x</span>)</span>;       <span class="comment">//声明委托类型</span></span><br><span class="line">MyDel delVar,dVar;                <span class="comment">//创建两个委托变量</span></span><br><span class="line"></span><br><span class="line">delVar= <span class="keyword">new</span> MyDel(myInstObj.MyM1);<span class="comment">//创建委托并保存引用</span></span><br><span class="line">dVar =<span class="keyword">new</span> MyDel(SClass.OtherM2);  <span class="comment">//创建委托并保存引用</span></span><br></pre></td></tr></table></figure></p>
<p><img src="/image/cSharp/cSharp37.png" alt=""><br>当然也可以这样创建：<br><figure class="highlight lisp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">MyDel delVar=new MyDel<span class="list">(<span class="keyword">myInstObj</span>.MyM1)</span><span class="comment">;</span></span><br><span class="line">MyDel dVar= new MyDel<span class="list">(<span class="keyword">SClass</span>.OtherM2)</span><span class="comment">;</span></span><br></pre></td></tr></table></figure></p>
<p>或者使用快捷语法：<br><figure class="highlight dns"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">MyDel delVar=myInstObj.MyM1<span class="comment">;</span></span><br><span class="line">MyDel dVar= SClass.OtherM2<span class="comment">;</span></span><br></pre></td></tr></table></figure></p>
<h3 id="委托赋值">委托赋值</h3><p>　　由于委托是引用类型，我们可以通过给它赋值来改变包含在委托变量中的引用，旧的委托对象会被GC回收。<br><figure class="highlight dns"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">MyDel delVar<span class="comment">;</span></span><br><span class="line">delVar = myInstObj.MyM1<span class="comment">;</span></span><br><span class="line">//重新赋值</span><br><span class="line">delVar = SClass.OtherM2<span class="comment">;</span></span><br></pre></td></tr></table></figure></p>
<p><img src="/image/cSharp/cSharp38.png" alt=""></p>
<h3 id="组合委托">组合委托</h3><p>　　上面描述的委托的调用列表中都只有一个方法，委托可以使用<strong>+</strong>运算符来“组合”。这个运算最终会创建一个新的委托，新委托的调用列表是两个被操作委托调用列表的组合。<br>　　<strong>组合委托</strong>的操作数委托没有被修改，当组合委托的操作发生时，参与运算的委托都没有发生改变，只是创建了一个新的委托。<strong>委托是恒定的，委托对象被创建后就不会再被改变。</strong><br><figure class="highlight protobuf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="constant">MyDel delA</span> = myInstObj.MyM1;</span><br><span class="line"><span class="constant">MyDel delB</span> = SClass.OtherM2;</span><br><span class="line"><span class="constant"></span><br><span class="line">MyDel delC</span> = delA + delB;    <span class="comment">//组合委托会组合操作数委托的调用列表</span></span><br></pre></td></tr></table></figure></p>
<p><img src="/image/cSharp/cSharp39.png" alt=""></p>
<h3 id="为委托增加和移除方法">为委托增加和移除方法</h3><p>　　<strong>增加方法</strong>：<br>　　虽然委托是不可变的，但是c#提供了看上去可以为委托增加方法的语法，我们可以通过使用“<strong>+=</strong>”运算符来为委托增加方法或者另一个委托。<br><figure class="highlight protobuf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="constant">MyDel delVar</span> = inst.MyM1;    <span class="comment">//创建委托并初始化</span></span><br><span class="line">delVar += SCl.m3;            <span class="comment">//增加方法</span></span><br><span class="line">delVar += X.Act;             <span class="comment">//增加方法</span></span><br></pre></td></tr></table></figure></p>
<p>　　<strong>在使用+=运算符时，实际上是创建了个新的委托，新委托的调用列表是左边的委托加上右边要添加的方法。</strong><br><img src="/image/cSharp/cSharp40.png" alt=""></p>
<p>　　<strong>移除方法</strong>：<br>　　可以使用<strong>-=</strong>运算符从委托移除方法。<br><code>delVar -= SCl.m3;    //从委托移除方法</code><br>　　与为委托增加方法一样，移除方法其实就是创建了一个新的委托。新的委托是就委托的副本——只是没有了已经移除方法的引用。<br>　　移除委托时需要注意：<br>　　■ 如果在调用列表中的方法有多个实例，<strong>-=</strong>运算符将从列表最后还是搜索，并且移除第一个与方法匹配的实例。<br>　　■ 试图删除委托中不存在的方法没有效果。<br>　　■ 试图调用空委托会抛出异常。<br>　　■ 我们可以通过把委托和null进行比较来判断委托的调用列表是否为空。<strong>如果调用列表为空，则委托是null</strong>。  </p>
<h3 id="调用委托">调用委托</h3><p>　　可以向调用方法一样简单的调用委托。<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">delegate <span class="keyword">void</span> <span class="title">MyDel</span> <span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line">MyDel delVar =inst.MyM1;</span><br><span class="line">delVar += SCl.m3;</span><br><span class="line">delVar += X.Act;</span><br><span class="line">   ....</span><br><span class="line">delVar(<span class="number">55</span>);    <span class="comment">//调用委托</span></span><br><span class="line">   ....</span><br></pre></td></tr></table></figure></p>
<p><img src="/image/cSharp/cSharp41.png" alt=""><br>　　当委托被调用时，它使用被调用时的相同参数来调用调用列表中的每一个方法。<br>　　如果在调用列表里有好多个相同的方法，当委托被调用时，每次在列表中遇到这个方法时都会调用一次。
　　</p>
<p><strong>调用不带返回值的委托示例</strong>：<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">Delegate</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//定义一个没有参数和返回值的委托</span></span><br><span class="line">    <span class="function"><span class="keyword">delegate</span> <span class="keyword">void</span> <span class="title">PrintFunction</span>(<span class="params"></span>)</span>;</span><br><span class="line">    <span class="keyword">class</span> <span class="title">Program</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="keyword">string</span>[] args</span>)</span><br><span class="line">        </span>&#123;</span><br><span class="line">            Test t = <span class="keyword">new</span> Test();</span><br><span class="line">            PrintFunction pf; <span class="comment">//创建一个空委托</span></span><br><span class="line">            pf = t.Print1;    <span class="comment">//初始化该委托</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">//给委托增加方法</span></span><br><span class="line">            pf += Test.Print2;</span><br><span class="line">            pf += t.Print1;</span><br><span class="line"></span><br><span class="line">            <span class="comment">//调用委托</span></span><br><span class="line">            <span class="keyword">if</span> (pf != <span class="keyword">null</span>)</span><br><span class="line">                pf();</span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">                Console.WriteLine(<span class="string">"该委托为空！"</span>);</span><br><span class="line">            Console.ReadKey();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">class</span> <span class="title">Test</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Print1</span>(<span class="params"></span>)</span><br><span class="line">        </span>&#123;</span><br><span class="line">            Console.WriteLine(<span class="string">"Print1--实例方法"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Print2</span>(<span class="params"></span>)</span><br><span class="line">        </span>&#123;</span><br><span class="line">            Console.WriteLine(<span class="string">"Print2--静态方法"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：<br><img src="/image/cSharp/cSharp42.png" alt=""><br>　　在上面的代码中，我们给委托增加了三个方法，其中有两个方法(Print1)是重复的，从运行结果可以看出，按照顺序，<code>Print1</code>被调用了2次。<br>　　<strong>调用委托其实就是调用了委托的调用列表中的所有方法。</strong></p>
<p><strong>调用带返回值的委托示例</strong>：<br>　　如果委托有返回值，并且在调用列表中有一个以上的方法存在，那么：<br>　　■ 调用列表中的最后一个方法的返回值就是委托调用返回的值。<br>　　■ 调用列表中所有其他方法的返回值都会被忽略。<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">Delegate2</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//创建一个具有返回值，没有参数的委托</span></span><br><span class="line">    <span class="function"><span class="keyword">delegate</span> <span class="keyword">int</span> <span class="title">MyDel</span>(<span class="params"></span>)</span>;</span><br><span class="line">    <span class="keyword">class</span> <span class="title">Program</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="keyword">string</span>[] args</span>)</span><br><span class="line">        </span>&#123;</span><br><span class="line">            Test t = <span class="keyword">new</span> Test();</span><br><span class="line">            MyDel mDel = t.Add2;</span><br><span class="line">            mDel += t.Add3;</span><br><span class="line">            mDel += t.Add2;</span><br><span class="line">            Console.WriteLine(<span class="string">"值为：&#123;0&#125;"</span>,mDel());</span><br><span class="line">            Console.ReadKey();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">class</span> <span class="title">Test</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">int</span> <span class="keyword">value</span> = <span class="number">5</span>;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">Add2</span>(<span class="params"></span>)</span><br><span class="line">        </span>&#123;</span><br><span class="line">            <span class="keyword">value</span> += <span class="number">2</span>;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">value</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">Add3</span>(<span class="params"></span>)</span><br><span class="line">        </span>&#123;</span><br><span class="line">            <span class="keyword">value</span> += <span class="number">3</span>;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">value</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：<br><img src="/image/cSharp/cSharp43.png" alt=""><br>　　■ 上面代码中的委托添加了三个方法，分别是<code>Add2</code>、<code>Add3</code>、<code>Add2</code>。<br>　　■ 在最后打印的时候调用了委托，执行的过程是：<img src="/image/cSharp/cSharp44.png" alt=""></p>
<p><strong>调用带引用参数的委托示例</strong><br>　　如果委托有引用参数，在调用委托列表中的下一个方法时，参数的新值(不是初始值)会传给下一个方法。以此类推。<br><figure class="highlight nimrod"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> <span class="type">System</span>;</span><br><span class="line"></span><br><span class="line">namespace <span class="type">Delegate3</span></span><br><span class="line">&#123;</span><br><span class="line">    delegate <span class="type">void</span> <span class="type">MyDel</span>(<span class="keyword">ref</span> <span class="type">int</span> x);</span><br><span class="line">    class <span class="type">Program</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">static</span> <span class="type">void</span> <span class="type">Main</span>(<span class="type">string</span>[] args)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="type">Test</span> t = new <span class="type">Test</span>();</span><br><span class="line">            <span class="type">MyDel</span> mDel = t.<span class="type">Add2</span>;</span><br><span class="line">            mDel += t.<span class="type">Add3</span>;</span><br><span class="line">            mDel += t.<span class="type">Add2</span>;</span><br><span class="line">            <span class="type">int</span> x = <span class="number">5</span>;</span><br><span class="line">            mDel(<span class="keyword">ref</span> x);</span><br><span class="line">            <span class="type">Console</span>.<span class="type">WriteLine</span>(<span class="string">"值为：&#123;0&#125;"</span>,x);</span><br><span class="line">            <span class="type">Console</span>.<span class="type">ReadKey</span>();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    class <span class="type">Test</span></span><br><span class="line">    &#123;</span><br><span class="line">        public <span class="type">void</span> <span class="type">Add2</span>(<span class="keyword">ref</span> <span class="type">int</span> x)</span><br><span class="line">        &#123;</span><br><span class="line">            x += <span class="number">2</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        public <span class="type">void</span> <span class="type">Add3</span>(<span class="keyword">ref</span> <span class="type">int</span> x)</span><br><span class="line">        &#123;</span><br><span class="line">            x += <span class="number">3</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：<br><img src="/image/cSharp/cSharp45.png" alt=""><br>　　■ 引用参数是按顺序在调用方法中一次传递的：<img src="/image/cSharp/cSharp46.png" alt=""></p>
<h3 id="匿名方法">匿名方法</h3><p>　　如果一个方法在调用列表中，或者说在委托的调用中只会被使用一次，那么则没有必要创建一个独立的具名方法，可以使用匿名方法。<br>　　<strong>匿名方法</strong>(anonymous metfod)是在初始化委托是内联声明的方法。</p>
<p><strong>具名方法代码示例：</strong><br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> Delegate4</span><br><span class="line">&#123;</span><br><span class="line">    <span class="function">delegate <span class="keyword">int</span> <span class="title">MyDel</span><span class="params">(<span class="keyword">int</span> x)</span></span>;</span><br><span class="line">    <span class="keyword">class</span> Program</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span><span class="params">(<span class="built_in">string</span>[] args)</span></span><br><span class="line">        </span>&#123;</span><br><span class="line">            MyDel md = Add;</span><br><span class="line"></span><br><span class="line">            Console.WriteLine(<span class="string">"具名方法的输出：&#123;0&#125;"</span>,md(<span class="number">5</span>));</span><br><span class="line">            Console.WriteLine(<span class="string">"具名方法的输出：&#123;0&#125;"</span>,md(<span class="number">6</span>));</span><br><span class="line">            Console.ReadKey();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">Add</span><span class="params">(<span class="keyword">int</span> x)</span></span><br><span class="line">        </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> x += <span class="number">20</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：<img src="/image/cSharp/cSharp47.png" alt=""><br><strong>匿名方法代码示例：</strong><br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">Delegate5</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="function"><span class="keyword">delegate</span> <span class="keyword">int</span> <span class="title">MyDel</span>(<span class="params"><span class="keyword">int</span> x</span>)</span>;</span><br><span class="line">    <span class="keyword">class</span> <span class="title">Program</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="keyword">string</span>[] args</span>)</span><br><span class="line">        </span>&#123;</span><br><span class="line">            MyDel md = <span class="keyword">delegate</span> (<span class="keyword">int</span> x)</span><br><span class="line">              &#123;</span><br><span class="line">                  <span class="keyword">return</span> x += <span class="number">20</span>;</span><br><span class="line">              &#125;;</span><br><span class="line">            Console.WriteLine(<span class="string">"匿名方法的输出：&#123;0&#125;"</span>,md(<span class="number">5</span>));</span><br><span class="line">            Console.WriteLine(<span class="string">"匿名方法的输出：&#123;0&#125;"</span>,md(<span class="number">6</span>));</span><br><span class="line">            Console.ReadKey();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：<img src="/image/cSharp/cSharp48.png" alt=""><br>　　可以看到两种方式都得到了相同的计算结果。<strong>一个方法只会被使用一次，这个方法初始化委托时可以使用匿名方法的形式。</strong></p>
<h4 id="在什么地方使用匿名方法：">在什么地方使用匿名方法：</h4><p>　　■ 声明委托变量时作为初始化表达式<br>　　■ 组合委托是在赋值语句的右边<br>　　■ 为委托增加事件时在赋值语句的右边</p>
<h4 id="匿名方法的语法：">匿名方法的语法：</h4><p><img src="/image/cSharp/cSharp49.png" alt=""><br>　　■ <strong>delegate</strong>类型关键字。<br>　　■ 参数列表，如果语句块没有使用任何参数则可以省略。<br>　　■ 语句块，它包含了匿名方法的代码。
　　</p>
<h5 id="1、返回类型：">1、返回类型：</h5><p>　　<strong>匿名方法不会显示的声明返回值</strong>。但是匿名方法的实现本身的行为必须一个类型上与委托返回值类型想匹配的值。<strong>如果委托有void类型的返回值，那么匿名方法就不能有返回值</strong>。<br>例如：<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">delegate</span> <span class="keyword">int</span> <span class="title">MyDel</span>(<span class="params"><span class="keyword">int</span> x</span>)</span>;     <span class="comment">//委托类型的返回值类型是int</span></span><br><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="keyword">string</span>[] args</span>)</span><br><span class="line"></span>&#123;</span><br><span class="line">    MuDel md = <span class="keyword">delegate</span> (<span class="keyword">int</span> x)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> x + <span class="number">20</span>; <span class="comment">//匿名方法也必须返回一个int类型的值</span></span><br><span class="line">            &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h5 id="2、参数">2、参数</h5><p>　　除了参数是数组的情况，匿名方法的参数列表必须在三个方面匹配委托：<br>　　■ 参数数量<br>　　■ 参数类型<br>　　■ 修饰符<br>　　<br>　　<strong>我们可以在委托的参数列表不包含任何out参数，并且匿名方法不使用任何参数的情况下，通过圆括号为空或者直接省略圆括号来简化匿名方法的参数列表。</strong><br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">delegate</span> <span class="keyword">void</span> <span class="title">MyDel</span>(<span class="params"><span class="keyword">int</span> x</span>)</span>; </span><br><span class="line"></span><br><span class="line"><span class="comment">//调用委托</span></span><br><span class="line">MyDel md = <span class="keyword">delegate</span>      <span class="comment">//省略的参数列表</span></span><br><span class="line">        &#123;</span><br><span class="line">            Console.WriteLine(<span class="string">"简化匿名方法！"</span>);  </span><br><span class="line">        &#125;;</span><br></pre></td></tr></table></figure></p>
<h5 id="3、params参数">3、params参数</h5><p>　　如果委托声明的参数列表中包含了params参数，那么使用匿名方法时，匿名方法的参数列表就会忽略params关键字。<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">delegate</span> <span class="keyword">void</span> <span class="title">MyDel</span>(<span class="params"><span class="keyword">int</span> x,<span class="keyword">params</span> <span class="keyword">int</span>[] y</span>)</span>;   <span class="comment">//在委托类型的声明中使用了params关键字</span></span><br><span class="line"></span><br><span class="line">MyDel md = <span class="keyword">delegate</span>(<span class="keyword">int</span> x,<span class="keyword">int</span>[] y)    <span class="comment">//在匹配的匿名方法中忽略了params关键字</span></span><br><span class="line">        &#123;</span><br><span class="line">           ......</span><br><span class="line">        &#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="变量和参数的作用域">变量和参数的作用域</h3><p>　　匿名方法的参数以及声明在匿名方法内部的局部变量的作用域限制在匿名方法的主体之内。<br><img src="/image/cSharp/cSharp50.png" alt=""></p>
<h5 id="1、外部变量">1、外部变量</h5><p>　　匿名方法可以访问它的外围作用域的局部变量和环境。<br>　　■ 外围作用域的变量叫做<strong>外部变量</strong>(outer variable)。<br>　　■ 用在匿名方法实现代码中的外部变量称为<strong>被方法捕获</strong>(captured)。<br><img src="/image/cSharp/cSharp51.png" alt=""></p>
<h5 id="2、被方法捕获变量的生命周期扩展">2、被方法捕获变量的生命周期扩展</h5><p><img src="/image/cSharp/cSharp52.png" alt=""><br>　　■ 局部变量x在块中声明和初始化<br>　　■ 委托mDel被匿名方法初始化，该匿名方法捕获了外部变量x<br>　　■ 当x离开块区域时，引用x的值会发生编译错误<br>　　■ 但是，委托mDel中的匿名方法在它的环境中保留了变量x，并且在mDel被调用时可以输出x的值。</p>
<h2 id="Lambda表达式">Lambda表达式</h2><p>　　c#2.0中引入了匿名方法，它允许我们在创建或者为委托增加方法时包含小段内联代码。<br>　　c#3.0中引入了lambda表达式，简化了匿名方法的语法。<br>　　<br>　　在匿名方法的语法中，delegate关键字是有点多余的，因为编译器已经知道我们在将方法赋值给委托，我们可以把匿名方法转换为lambda表达式。<br>　　■ 删除delegate关键字<br>　　■ 在参数列表和匿名方法主体之间放lambda运算符“<strong>=&gt;</strong>”<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">MyDel md = delegate(<span class="keyword">int</span> x)    &#123;<span class="keyword">return</span> x + <span class="number">1</span>&#125;;   <span class="comment">//匿名方法</span></span><br><span class="line">MyDel md =         (<span class="keyword">int</span> x) =&gt; &#123;<span class="keyword">return</span> x + <span class="number">1</span>&#125;;   <span class="comment">//lambda表达式</span></span><br></pre></td></tr></table></figure></p>
<p>　　编译器通过推断允许我们更进一步简化lambda表达式：<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">MyDel md  = delegate(<span class="keyword">int</span> x)    &#123;<span class="keyword">return</span> x + <span class="number">1</span>&#125;;   <span class="comment">//匿名方法</span></span><br><span class="line">MyDel md2 =         (<span class="keyword">int</span> x) =&gt; &#123;<span class="keyword">return</span> x + <span class="number">1</span>&#125;;   <span class="comment">//lambda表达式</span></span><br><span class="line">MyDel md3 =             (x) =&gt; &#123;<span class="keyword">return</span> x + <span class="number">1</span>&#125;;   <span class="comment">//lambda表达式</span></span><br><span class="line">MyDel md4 =              x  =&gt; &#123;<span class="keyword">return</span> x + <span class="number">1</span>&#125;;   <span class="comment">//lambda表达式</span></span><br><span class="line">MyDel md5 =              x  =&gt;         x + <span class="number">1</span> ;   <span class="comment">//lambda表达式</span></span><br></pre></td></tr></table></figure></p>
<p>　　■ 编译器还可以从委托的声明中知道委托参数的类型，因此lambda表达式允许我们省略类型参数，如上面代码中<code>md3</code>的赋值代码所示。<br>　　　　□ 带有类型的参数列表称为显式类型<br>　　　　□ 省略类型的参数列表称为隐式类型<br>　　■ 如果只有一个隐式类型的参数，我们可以省略周围的圆括号，如上面代码中的<code>md4</code>赋值代码所示。<br>　　■ <strong>lambda表达式允许表达式的主体是语句块或表达式</strong>。如果语句块包含了一个返回语句(return)，我们可以将语句块中的return关键字省略，直接写表达式。如<code>md5</code>的赋值代码所示。</p>
<p>　　有关lambda表达式的参数列表的要点如下：<br>　　■ lambda表达式参数列表中的参数必须在参数数量、类型和位置上与委托相匹配。<br>　　■ 表达式的参数列表中的参数不一定需要包含类型(比如隐式类型)，除非委托有<code>ref</code>或者<code>out</code>参数，此时声明参数类型是必须的(比如显式类型)。<br>　　■ 如果只有一个参数，并且是隐式类型，周围的圆括号可以被省略，否则圆括号就是必须写上的。<br>　　■ 如果没有参数，则必须使用一组空的圆括号代替。</p>

      
    </div>

    
      

    
    <br/><br/>
<!--baidu share-->

<div class="bdsharebuttonbox"><a class="bds_more" href="#" data-cmd="more">分享到：</a><a title="分享到新浪微博" class="bds_tsina" href="#" data-cmd="tsina">新浪微博</a><a title="分享到微信" class="bds_weixin" href="#" data-cmd="weixin">微信</a><a title="分享到QQ空间" class="bds_qzone" href="#" data-cmd="qzone">QQ空间</a><a title="分享到QQ好友" class="bds_sqq" href="#" data-cmd="sqq">QQ好友</a></div>
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{"bdSize":16}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
  
<!--baidu share-->
    <footer class="article-footer">
      <a data-url="/2015/05/20/c#-delegate/" data-id="ciqvzmhp3002ncsvl1i05aga4" class="article-share-link">
        <i class="fa fa-share"></i> Share
      </a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/c-学习笔记/">c#学习笔记</a></li></ul>


    </footer>
  </div>
  
    
<ul id="article-nav" class="nav nav-pills nav-justified">
  
  <li role="presentation">
    <a href="/2015/05/19/Exceptions-try-statements/" id="article-nav-older" class="article-nav-link-wrap">
      <i class="fa fa-chevron-left pull-left"></i>
      <span class="article-nav-link-title">异常(try语句)</span>
    </a>
  </li>
  
  
  <li role="presentation">
    <a href="/2015/05/21/c#-Event/" id="article-nav-newer" class="article-nav-link-wrap">
      <span class="article-nav-link-title">事件(Event)</span>
      <i class="fa fa-chevron-right pull-right"></i>
    </a>
  </li>
  
</ul>


  
</article>


<section id="comments">
  <div id="disqus_thread">
    <!-- 多说评论框 start -->
	<div class="ds-thread" data-thread-key="2015/05/20/c#-delegate/" data-title="委托(delegate)" data-url="/2015/05/20/c#-delegate/"></div>
<!-- 多说评论框 end -->
<!-- 多说公共JS代码 start (一个网页只需插入一次) -->
<script type="text/javascript">
var duoshuoQuery = {short_name:"hais2"};
	(function() {
		var ds = document.createElement('script');
		ds.type = 'text/javascript';ds.async = true;
		ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
		ds.charset = 'UTF-8';
		(document.getElementsByTagName('head')[0] 
		 || document.getElementsByTagName('body')[0]).appendChild(ds);
	})();
	</script>
<!-- 多说公共JS代码 end -->
  </div>
</section>


        </div>
        <div class="col-sm-3 col-sm-offset-1 blog-sidebar">
          
  <div class="sidebar-module sidebar-module-inset">
<a style="text-decoration:none" title="有本事点我一下试试.." href='javascript:(function() {
	function c() {
		var e = document.createElement("link");
		e.setAttribute("type", "text/css");
		e.setAttribute("rel", "stylesheet");
		e.setAttribute("href", f);
		e.setAttribute("class", l);
		document.body.appendChild(e)
	}
 
	function h() {
		var e = document.getElementsByClassName(l);
		for (var t = 0; t < e.length; t++) {
			document.body.removeChild(e[t])
		}
	}
 
	function p() {
		var e = document.createElement("div");
		e.setAttribute("class", a);
		document.body.appendChild(e);
		setTimeout(function() {
			document.body.removeChild(e)
		}, 100)
	}
 
	function d(e) {
		return {
			height : e.offsetHeight,
			width : e.offsetWidth
		}
	}
 
	function v(i) {
		var s = d(i);
		return s.height > e && s.height < n && s.width > t && s.width < r
	}
 
	function m(e) {
		var t = e;
		var n = 0;
		while (!!t) {
			n += t.offsetTop;
			t = t.offsetParent
		}
		return n
	}
 
	function g() {
		var e = document.documentElement;
		if (!!window.innerWidth) {
			return window.innerHeight
		} else if (e && !isNaN(e.clientHeight)) {
			return e.clientHeight
		}
		return 0
	}
 
	function y() {
		if (window.pageYOffset) {
			return window.pageYOffset
		}
		return Math.max(document.documentElement.scrollTop, document.body.scrollTop)
	}
 
	function E(e) {
		var t = m(e);
		return t >= w && t <= b + w
	}
 
	function S() {
		var e = document.createElement("audio");
		e.setAttribute("class", l);
		e.src = i;
		e.loop = false;
		e.addEventListener("canplay", function() {
			setTimeout(function() {
				x(k)
			}, 500);
			setTimeout(function() {
				N();
				p();
				for (var e = 0; e < O.length; e++) {
					T(O[e])
				}
			}, 15500)
		}, true);
		e.addEventListener("ended", function() {
			N();
			h()
		}, true);
		e.innerHTML = " <p>If you are reading this, it is because your browser does not support the audio element. We recommend that you get a new browser.</p> <p>";
		document.body.appendChild(e);
		e.play()
	}
 
	function x(e) {
		e.className += " " + s + " " + o
	}
 
	function T(e) {
		e.className += " " + s + " " + u[Math.floor(Math.random() * u.length)]
	}
 
	function N() {
		var e = document.getElementsByClassName(s);
		var t = new RegExp("\\b" + s + "\\b");
		for (var n = 0; n < e.length; ) {
			e[n].className = e[n].className.replace(t, "")
		}
	}
 
	var e = 30;
	var t = 30;
	var n = 350;
	var r = 350;
	var i = "//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake.mp3";
	var s = "mw-harlem_shake_me";
	var o = "im_first";
	var u = ["im_drunk", "im_baked", "im_trippin", "im_blown"];
	var a = "mw-strobe_light";
	var f = "//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake-style.css";
	var l = "mw_added_css";
	var b = g();
	var w = y();
	var C = document.getElementsByTagName("*");
	var k = null;
	for (var L = 0; L < C.length; L++) {
		var A = C[L];
		if (v(A)) {
			if (E(A)) {
				k = A;
				break
			}
		}
	}
	if (A === null) {
		console.warn("Could not find a node of the right size. Please try a different page.");
		return
	}
	c();
	S();
	var O = [];
	for (var L = 0; L < C.length; L++) {
		var A = C[L];
		if (v(A)) {
			O.push(A)
		}
	}
})()'>
    <h4>Hi</h4>
</a>

  
  <h5>我是海建东,欢迎访问我的博客.</h5>

</div>


  
  <div class="sidebar-module">
    <h4>分类</h4>
    <ul class="sidebar-module-list"><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/categories/ASP-NET/">ASP.NET</a><span class="sidebar-module-list-count">4</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/categories/ASP-NET-Web-API/">ASP.NET Web API</a><span class="sidebar-module-list-count">1</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/categories/Asp-Net-MVC/">Asp.Net MVC</a><span class="sidebar-module-list-count">3</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/categories/Git/">Git</a><span class="sidebar-module-list-count">3</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/categories/Hexo/">Hexo</a><span class="sidebar-module-list-count">10</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/categories/SQL/">SQL</a><span class="sidebar-module-list-count">8</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/categories/UWP/">UWP</a><span class="sidebar-module-list-count">2</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/categories/Umbraco/">Umbraco</a><span class="sidebar-module-list-count">11</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/categories/c/">c#</a><span class="sidebar-module-list-count">18</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/categories/前端开发/">前端开发</a><span class="sidebar-module-list-count">3</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/categories/文集/">文集</a><span class="sidebar-module-list-count">12</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/categories/设计模式/">设计模式</a><span class="sidebar-module-list-count">6</span></li></ul>
  </div>



  
  <div class="sidebar-module">
    <h4>标签</h4>
    <ul class="sidebar-module-list"><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/ASP-NET/">ASP.NET</a><span class="sidebar-module-list-count">1</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/ASP-NET-Core/">ASP.NET Core</a><span class="sidebar-module-list-count">2</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/ASP-NET-Web-API笔记/">ASP.NET Web API笔记</a><span class="sidebar-module-list-count">1</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/AngularJS/">AngularJS</a><span class="sidebar-module-list-count">2</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/Asp-Net-MVC/">Asp.Net MVC</a><span class="sidebar-module-list-count">3</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/EntityFramework/">EntityFramework</a><span class="sidebar-module-list-count">1</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/Git学习/">Git学习</a><span class="sidebar-module-list-count">3</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/Hexo博客搭建/">Hexo博客搭建</a><span class="sidebar-module-list-count">10</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/SQL-Server/">SQL Server</a><span class="sidebar-module-list-count">8</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/UWP学习笔记/">UWP学习笔记</a><span class="sidebar-module-list-count">2</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/Umbraco入门/">Umbraco入门</a><span class="sidebar-module-list-count">11</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/WebForm/">WebForm</a><span class="sidebar-module-list-count">1</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/c-基础知识/">c#基础知识</a><span class="sidebar-module-list-count">6</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/c-学习笔记/">c#学习笔记</a><span class="sidebar-module-list-count">12</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/jQuery/">jQuery</a><span class="sidebar-module-list-count">1</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/影评：其实就是观后感/">影评：其实就是观后感</a><span class="sidebar-module-list-count">4</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/设计模式学习笔记/">设计模式学习笔记</a><span class="sidebar-module-list-count">6</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/转载/">转载</a><span class="sidebar-module-list-count">7</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/tags/随笔/">随笔</a><span class="sidebar-module-list-count">8</span></li></ul>
  </div>



  
  <div class="sidebar-module">
    <h4>归档</h4>
    <ul class="sidebar-module-list"><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/archives/2016/07/">七月 2016</a><span class="sidebar-module-list-count">4</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/archives/2016/05/">五月 2016</a><span class="sidebar-module-list-count">3</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/archives/2016/04/">四月 2016</a><span class="sidebar-module-list-count">2</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/archives/2016/03/">三月 2016</a><span class="sidebar-module-list-count">14</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/archives/2016/02/">二月 2016</a><span class="sidebar-module-list-count">6</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/archives/2016/01/">一月 2016</a><span class="sidebar-module-list-count">23</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/archives/2015/10/">十月 2015</a><span class="sidebar-module-list-count">2</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/archives/2015/09/">九月 2015</a><span class="sidebar-module-list-count">1</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/archives/2015/08/">八月 2015</a><span class="sidebar-module-list-count">1</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/archives/2015/07/">七月 2015</a><span class="sidebar-module-list-count">2</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/archives/2015/06/">六月 2015</a><span class="sidebar-module-list-count">4</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/archives/2015/05/">五月 2015</a><span class="sidebar-module-list-count">7</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/archives/2015/04/">四月 2015</a><span class="sidebar-module-list-count">6</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/archives/2015/03/">三月 2015</a><span class="sidebar-module-list-count">2</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/archives/2015/02/">二月 2015</a><span class="sidebar-module-list-count">1</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/archives/2015/01/">一月 2015</a><span class="sidebar-module-list-count">1</span></li><li class="sidebar-module-list-item"><a class="sidebar-module-list-link" href="/archives/2014/11/">十一月 2014</a><span class="sidebar-module-list-count">2</span></li></ul>
  </div>



  <!---->
  <div class="sidebar-module">
    <h4>最近访客</h4>
    <!--<ul class="sidebar-module-list">
      
        <li>
          <a href="/2016/07/19/Journey-Under-the-Midnight-Sun/">行走在白昼的夜里</a>
        </li>
      
        <li>
          <a href="/2016/07/18/Hexo-misspelled-tag/">Hexo标签分类写错的处理</a>
        </li>
      
        <li>
          <a href="/2016/07/17/Value-types-and-reference-types/">值类型和引用类型</a>
        </li>
      
        <li>
          <a href="/2016/07/17/Fiddler-caught-analysis-API/">使用Fiddler抓包分析API</a>
        </li>
      
        <li>
          <a href="/2016/05/14/Hexo-Getting-Started-1/">8、使用SSH方式部署博客</a>
        </li>
      
    </ul>
    
    <div class="ds-recent-visitors" data-num-items="12" data-avatar-size="28" id="ds-recent-visitors"></div>
-->
<!-- 多说最新评论 start -->
	<div class="ds-recent-comments" data-num-items="5" data-show-avatars="1" data-show-time="1" data-show-title="1" data-show-admin="0" data-excerpt-length="20"></div>
<!-- 多说最新评论 end -->
<!-- 多说公共JS代码 start (一个网页只需插入一次) -->
<script type="text/javascript">
var duoshuoQuery = {short_name:"hais2"};
	(function() {
		var ds = document.createElement('script');
		ds.type = 'text/javascript';ds.async = true;
		ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
		ds.charset = 'UTF-8';
		(document.getElementsByTagName('head')[0] 
		 || document.getElementsByTagName('body')[0]).appendChild(ds);
	})();
	</script>
<!-- 多说公共JS代码 end -->
<br><br>
<!---->

  
  <div class="sidebar-module">
    <h4>标签云</h4>
    <p class="tagcloud">
      <a href="/tags/ASP-NET/" style="font-size: 10px;">ASP.NET</a> <a href="/tags/ASP-NET-Core/" style="font-size: 11.11px;">ASP.NET Core</a> <a href="/tags/ASP-NET-Web-API笔记/" style="font-size: 10px;">ASP.NET Web API笔记</a> <a href="/tags/AngularJS/" style="font-size: 11.11px;">AngularJS</a> <a href="/tags/Asp-Net-MVC/" style="font-size: 12.22px;">Asp.Net MVC</a> <a href="/tags/EntityFramework/" style="font-size: 10px;">EntityFramework</a> <a href="/tags/Git学习/" style="font-size: 12.22px;">Git学习</a> <a href="/tags/Hexo博客搭建/" style="font-size: 17.78px;">Hexo博客搭建</a> <a href="/tags/SQL-Server/" style="font-size: 16.67px;">SQL Server</a> <a href="/tags/UWP学习笔记/" style="font-size: 11.11px;">UWP学习笔记</a> <a href="/tags/Umbraco入门/" style="font-size: 18.89px;">Umbraco入门</a> <a href="/tags/WebForm/" style="font-size: 10px;">WebForm</a> <a href="/tags/c-基础知识/" style="font-size: 14.44px;">c#基础知识</a> <a href="/tags/c-学习笔记/" style="font-size: 20px;">c#学习笔记</a> <a href="/tags/jQuery/" style="font-size: 10px;">jQuery</a> <a href="/tags/影评：其实就是观后感/" style="font-size: 13.33px;">影评：其实就是观后感</a> <a href="/tags/设计模式学习笔记/" style="font-size: 14.44px;">设计模式学习笔记</a> <a href="/tags/转载/" style="font-size: 15.56px;">转载</a> <a href="/tags/随笔/" style="font-size: 16.67px;">随笔</a>
    </p>
  </div>


  
  <div class="sidebar-module">
     <iframe width="100%" height="550" class="share_self"  frameborder="0" scrolling="no" src="http://widget.weibo.com/weiboshow/index.php?language=&width=0&height=550&fansRow=2&ptype=1&speed=0&skin=5&isTitle=1&noborder=0&isWeibo=1&isFans=1&uid=3311041262&verifier=85e63399&dpc=1"></iframe>
  </div>


  
  <div class="sidebar-module">
    <h4>友情链接</h4>
    <a href="http://www.cnblogs.com/artech/" target="_blank">Artech</a><br/>
    <a href="http://www.deepthink.pub" target="_blank">DeepThink</a><br/>
    <a href="http://edi.wang/" target="_blank">Edi.wang</a><br/>
    <a href="http://www.lanhusoft.com/Article/List" target="_blank">LanHuSoft</a><br/>
    <a href="http://www.ruanyifeng.com/blog/" target="_blank">阮一峰的网络日志</a><br/>
    <a href="http://www.cnblogs.com/shanyou/" target="_blank">张善友</a><br/>
  </div>




        </div>
    </div>
  </div>
  <footer class="blog-footer">
  <div class="container">
    <div id="footer-info" class="inner">
      &copy; 2016 Is2<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a> and <a href="https://github.com/haijiandong/haijiandong.github.io" target="_blank">GitHub</a><br/>
      Total <span id="busuanzi_value_site_pv"></span> views.  Total <span id="busuanzi_value_site_uv"></span> guests.  Total <span id="busuanzi_value_page_pv"></span> Hits.
    </div>
  </div>
</footer>

  

<script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>


<script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css" type="text/css">
  <script src="/fancybox/jquery.fancybox.pack.js" type="text/javascript"></script>


<script src="/js/script.js" type="text/javascript"></script>

</body>
</html>